#include "stdafx.h"
#include "InputManager.h"

namespace Input
{
	//--------------------------------------------------------------------
	//--------------------------------------------------------------------
	//--------------------------------------------------------------------

	InputManager InputManager::s_oManager;

	InputManager::InputManager()
	:	m_pDI(NULL),
		m_pKeyboard(NULL),
		m_pMouse(NULL)
	{
	}

	InputManager::~InputManager()
	{

	}

	InputManager& InputManager::GetInstance()
	{
		return s_oManager;
	}

	bool InputManager::Create(HINSTANCE& _hInst, HWND& _hWnd, DWORD _uCooperativeLevel)
	{
		HRESULT hResult = DirectInput8Create( _hInst, DIRECTINPUT_VERSION, IID_IDirectInput8, (void**)&m_pDI, NULL );
		if ( FAILED( hResult ) )
		{ 
			return false;
		}

		// keyboard
		hResult = m_pDI->CreateDevice( GUID_SysKeyboard, &m_pKeyboard, NULL );
		if ( FAILED( hResult ) )
		{ 
			return false;
		}
		hResult = m_pKeyboard->SetDataFormat( &c_dfDIKeyboard ); 
		if ( FAILED( hResult ) )
		{ 
			return false;
		}
		hResult = m_pKeyboard->SetCooperativeLevel( _hWnd, _uCooperativeLevel );
		if ( FAILED( hResult ) )
		{ 
			return false;
		}

		// mouse
		hResult = m_pDI->CreateDevice( GUID_SysMouse, &m_pMouse, NULL );
		if ( FAILED( hResult ) )
		{ 
			return false;
		}
		hResult = m_pMouse->SetDataFormat( &c_dfDIMouse2 ); 
		if ( FAILED( hResult ) )
		{ 
			return false;
		}
		hResult = m_pMouse->SetCooperativeLevel( _hWnd, _uCooperativeLevel );
		if ( FAILED( hResult ) )
		{ 
			return false;
		}

		return true;
	}

	void InputManager::Release()
	{
		if ( NULL != m_pKeyboard )
		{
			m_pKeyboard->Release();
			m_pKeyboard = NULL;
		}
		if ( NULL != m_pMouse )
		{
			m_pMouse->Release();
			m_pMouse= NULL;
		}
		if ( NULL != m_pDI )
		{
			m_pDI->Release();
			m_pDI= NULL;
		}
	}

	void InputManager::Update()
	{
		if ( NULL != m_pKeyboard )
		{
			if ( SUCCEEDED( m_pKeyboard->Acquire() ) )
			{
				memcpy( &m_oOldRawKeyboardInfo, &m_oRawKeyboardInfo, sizeof( RawKeyboardInfo ) );
				m_pKeyboard->GetDeviceState( sizeof( RawKeyboardInfo ),(LPVOID)&m_oRawKeyboardInfo );
			}
		}
		if ( NULL != m_pMouse )
		{
			if ( SUCCEEDED( m_pMouse->Acquire() ) )
			{
				memcpy( &m_oOldRawMouseInfo, &m_oRawMouseInfo, sizeof( RawMouseInfo ) );
				m_pMouse->GetDeviceState( sizeof( RawMouseInfo ),(LPVOID)&m_oRawMouseInfo );
			}
		}
	}

	void InputManager::GetRawKeyboardInfo(RawKeyboardInfo& _rInfo)
	{
		memcpy( &_rInfo, &m_oRawKeyboardInfo, sizeof( RawKeyboardInfo ) );
	}

	void InputManager::GetRawMouseInfo(RawMouseInfo& _rInfo)
	{
		memcpy( &_rInfo, &m_oRawMouseInfo, sizeof( RawMouseInfo ) );
	}

	bool InputManager::JustReleasedKey(const unsigned int& _uDIK) const
	{
		return ( ( 0x0 == ( 0x80 & m_oRawKeyboardInfo[_uDIK] ) ) &&  ( 0x0 != ( 0x80 & m_oOldRawKeyboardInfo[_uDIK] ) ) );
	}

	bool InputManager::JustPressedKey(const unsigned int& _uDIK) const
	{
		return ( ( 0x0 != ( 0x80 & m_oRawKeyboardInfo[_uDIK] ) ) &&  ( 0x0 == ( 0x80 & m_oOldRawKeyboardInfo[_uDIK] ) ) );
	}
}
